use super::enums::*;

#[derive(Debug, Clone, Copy)]
pub struct Pixel {
    pub is_what: IsWhat,            //像素块类别（空，头，身，尾，果）
    pub next_way: Direction,        //像素块下步移动方向（如果是蛇）
    pub loc: (usize, usize),        //像素块二维坐标（横，纵）   
    pub follow: (usize, usize),     //前块二维坐标（横，纵）（如果有）
    size: (usize, usize)
}
//初始化
impl Pixel {
    //初始化为空白块
    pub fn new(x: usize, y: usize, xs:usize, ys:usize) -> Pixel {
        //返回值
        Pixel {
            is_what: IsWhat::Block,
            next_way: Direction::Stop,
            loc: (x, y),
            follow: (x, y),
            size: (xs, ys)
        }
    }
    //刷新跟随坐标Pixel.follow,遇墙穿墙
    fn reset_follow(&mut self) {
        self.follow = self.loc;
        match self.next_way {
            Direction::Left => {
                self.follow.1 = self.loc.1 - 1;
                if self.follow.1 == 0 { self.follow.1 = self.size.1 - 2 };
            }
            Direction::Right => {
                self.follow.1 = self.loc.1 + 1;
                if self.follow.1 == self.size.1 - 1 { self.follow.1 = 1 };
            }
            Direction::Up => {
                self.follow.0 = self.loc.0 - 1;
                if self.follow.0 == 0 { self.follow.0 = self.size.0 - 2 };
            }
            Direction::Down => {
                self.follow.0 = self.loc.0 + 1;
                if self.follow.0 == self.size.0 - 1 { self.follow.0 = 1 };
            }
            Direction::Stop => {}
        }
    }
    //刷新空白块坐标
    pub fn init_block(&mut self,_x: usize,_y: usize) {
        //返回值
        self.loc = (_x, _y);
        self.reset_follow();
    }
}
//改写类
impl Pixel {
    //设为空白块
    pub fn set_block(&mut self) {
        self.is_what = IsWhat::Block;
        self.next_way = Direction::Stop;
        self.follow = self.loc;
    }
    //设为左边
    pub fn set_framel(&mut self) {
        self.is_what = IsWhat::Framel;
        self.next_way = Direction::Stop;
        self.follow = self.loc;
    }
    //设为左边
    pub fn set_framer(&mut self) {
        self.is_what = IsWhat::Framer;
        self.next_way = Direction::Stop;
        self.follow = self.loc;
    }
    //设为上下边
    pub fn set_framey(&mut self) {
        self.is_what = IsWhat::Framey;
        self.next_way = Direction::Stop;
        self.follow = self.loc;
    }
    //设为果实
    pub fn set_food(&mut self) {
        self.is_what = IsWhat::Food;
        self.next_way = Direction::Stop;
        self.follow = self.loc;
    }


    //设为蛇尾
    pub fn set_tail(&mut self, way: Direction) {   
        self.next_way = way;
        self.is_what = IsWhat::Tail;
        self.reset_follow();
    }
    //改为蛇尾
    pub fn body_to_tail(&mut self) {   
        self.is_what = IsWhat::Tail;
        self.reset_follow();
    }
    //设为蛇身
    pub fn set_body(&mut self, way: Direction) {
        self.next_way = way;
        self.is_what = IsWhat::Body;
        self.reset_follow();
    }
    //改为蛇身
    pub fn head_to_body(&mut self) {
        self.is_what = IsWhat::Body;
        self.reset_follow();
    }
    //设为蛇头
    pub fn set_head(&mut self,way: Direction) {
        self.next_way = way;
        self.is_what = IsWhat::Head;
        self.reset_follow();
    }
    //改写移动方向
    pub fn set_way(&mut self, way: Direction) {
        self.next_way = way;
        self.reset_follow();
    }
}
//读取类
impl Pixel {     
    //打印当前块
    pub fn print(&self) {
        //使用枚举的打印块符号方法
        self.is_what.print();            
    }
    //获取位置
    pub fn get_loc(self) -> (usize, usize) {
        self.loc
    }
}



